//
//  AppDelegate.swift
//  WeiBoOfSwift
//
//  Created by aven wu on 19/04/2018.
//  Copyright © 2018 GML. All rights reserved.
//

import UIKit
import SVProgressHUD

var KTC_SCREEN_RATION:CGFloat = 1.0;
var SAFE_SCREEN_TOP:CGFloat = 20.0; // iPhoneX顶部不可编辑高度
var SAFE_SCREEN_BOTTOM:CGFloat = 0.0;// iPhoneX底部不可编辑高度

// 切换控制器通知
let SwitchRootViewControllerKey = "SwitchRootViewControllerKey"

@UIApplicationMain

class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?


    func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
        // 创建window
        window = UIWindow(frame: UIScreen.main.bounds)
        window?.backgroundColor = UIColor.white
        window?.rootViewController = WelcomeViewController()
        window?.makeKeyAndVisible()
        // 设置导航条和工具条的外观
        // 因为外观一旦设置全局有效, 所以应该在程序一进来就设置
        UINavigationBar.appearance().tintColor = UIColor.orange
        UITabBar.appearance().tintColor = UIColor.orange
        
        // 统一设置SVProgressHUD外观
        SVProgressHUD.setForegroundColor(UIColor.white)
        SVProgressHUD.setBackgroundColor(UIColor(white: 0.0, alpha: 0.6))
        SVProgressHUD.setDefaultStyle(SVProgressHUDStyle.custom);
        
        // 以6s/6的尺寸为标准
        KTC_SCREEN_RATION = CGFloat(UIScreen.main.bounds.size.width) / 375.0;
        if #available(iOS 11.0, *) {
            SAFE_SCREEN_BOTTOM = (window?.safeAreaInsets.bottom)!
            SAFE_SCREEN_TOP = (window?.safeAreaInsets.top)! + 44

        } else {
            
        }
        // 注册一个通知
        NotificationCenter.default.addObserver(self, selector: #selector(AppDelegate.switchRootViewController(notify:)), name: NSNotification.Name(rawValue: SwitchRootViewControllerKey), object: nil)
        
        return true
    }
    @objc func switchRootViewController(notify: Notification) -> Void {
        
        if notify.object as! Bool
        {
            window?.rootViewController = MainViewController()
        }else
        {
            window?.rootViewController = WelcomeViewController()
        }
    }
    /**
     用于获取默认界面
     
     :returns: 默认界面
     */
    private func defaultContoller() ->UIViewController
    {
        // 1.检测用户是否登录
        if UserOauthModel.userLogin(){
            return isNewupdate() ? NewFeatureCollectionViewController() : WelcomeViewController()
        }
        return MainViewController()
    }

    private func isNewupdate() -> Bool{
        // 1.获取当前软件的版本号 --> info.plist
        let currentVersion = Bundle.main.infoDictionary!["CFBundleShortVersionString"] as! String
        
        // 2.获取以前的软件版本号 --> 从本地文件中读取(以前自己存储的)
        let sandboxVersion =  UserDefaults.standard.object(forKey: "CFBundleShortVersionString") as? String ?? ""
        
        print("current = \(currentVersion) sandbox = \(sandboxVersion)")
        
        // 3.比较当前版本号和以前版本号
        //   2.0                    1.0
        if currentVersion.compare(sandboxVersion) == ComparisonResult.orderedDescending
        {
            // 3.1如果当前>以前 --> 有新版本
            // 3.1.1存储当前最新的版本号
            // iOS7以后就不用调用同步方法了
            UserDefaults.standard.set(currentVersion, forKey: "CFBundleShortVersionString")
            return true
        }
        // 3.2如果当前< | ==  --> 没有新版本
        return false
    }
    
    
    func applicationDidEnterBackground(_ application: UIApplication) {
        // Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
        // If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
    }

    func applicationWillEnterForeground(_ application: UIApplication) {
        // Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
    }

    func applicationDidBecomeActive(_ application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
    }

    func applicationWillTerminate(_ application: UIApplication) {
        // Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
    }
    deinit{
        NotificationCenter.default.removeObserver(self)
    }


}

